<template>
  <div class="table-page">
    <GiTable row-key="id" title="内容管理" :data="dataList" :columns="columns" :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }" :pagination="pagination" :disabled-tools="['size']"
      :disabled-column-keys="['name']" @refresh="search">
      <template #toolbar-left>
        <a-input v-model="queryForm.title" placeholder="请输入标题" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-tree-select placeholder="请选择分类" allow-clear :data="categoryList" />
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #toolbar-right>
        <a-button v-permission="['content:content:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
      </template>
      <template #title="{ record }">
        <a-link @click="onDetail(record)">
          <a-typography-paragraph class="link-text" :ellipsis="{
            rows: 1,
            showTooltip: true,
            css: true,
          }">
            {{ record.title }}
          </a-typography-paragraph>
        </a-link>
      </template>
      <template #status="{ record }">
        <GiCellTag :value="record.status" :dict="notice_status_enum" />
      </template>
      <template #action="{ record }">
        <a-space>
          <!-- <a-link v-permission="['content:content:list']" title="查看" @click="onDetail(record)">查看</a-link> -->
          <a-link v-permission="['content:content:update']" title="修改" @click="onUpdate(record)">修改</a-link>
          <a-link v-permission="['content:content:delete']" status="danger" :disabled="record.disabled" title="删除"
            @click="onDelete(record)">
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <ContentAddModal ref="ContentAddModalRef" @save-success="search" />
    <ContentDetailDrawer ref="ContentDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import { deleteContent, listContent, type ContentQuery, type ContentResp } from '@/apis/website';
import type { TableInstanceColumns } from '@/components/GiTable/type';
import { useTable } from '@/hooks';
import { useCategory, useDict } from '@/hooks/app';
import { isMobile } from '@/utils';
import has from '@/utils/has';
import { useRouter } from 'vue-router';
import ContentAddModal from './ContentAddModal.vue';
import ContentDetailDrawer from './ContentDetailDrawer.vue';

defineOptions({ name: 'Content' })

const router = useRouter()

const { categoryList, getCategoryList } = useCategory()
const { notice_status_enum } = useDict('notice_status_enum')

if (!categoryList.value.length) {
  getCategoryList()
}

const queryForm = reactive<ContentQuery>({
  title: undefined,
  categoryId: undefined,
  sort: undefined,
  status: undefined,
  createUser: undefined,
  createTime: undefined
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listContent({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  {
    title: '序号',
    width: 66,
    align: 'center',
    render: ({ rowIndex }) => h('span', {}, rowIndex + 1 + (pagination.current - 1) * pagination.pageSize),
  },
  { title: '标题', dataIndex: 'title', slotName: 'title', minWidth: 200, ellipsis: true, tooltip: true },
  { title: '简介', dataIndex: 'introduction', slotName: 'introduction', minWidth: 200, ellipsis: true, tooltip: true },
  { title: '类型', dataIndex: 'categoryName', slotName: 'categoryName', align: 'center' },
  { title: '状态', dataIndex: 'status', slotName: 'status', align: 'center' },
  { title: '创建人', dataIndex: 'createUserString', slotName: 'createUserString' },
  { title: '创建时间', dataIndex: 'createTime', slotName: 'createTime' },
  // { title: '修改人', dataIndex: 'updateUser', slotName: 'updateUser' },
  // { title: '修改时间', dataIndex: 'updateTime', slotName: 'updateTime' },
  {
    title: '操作',
    slotName: 'action',
    width: 180,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['content:content:update', 'content:content:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.title = undefined
  queryForm.categoryId = undefined
  queryForm.sort = undefined
  queryForm.status = undefined
  queryForm.createUser = undefined
  queryForm.createTime = undefined
  search()
}

// 删除
const onDelete = (record: ContentResp) => {
  return handleDelete(() => deleteContent(record.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

const ContentAddModalRef = ref<InstanceType<typeof ContentAddModal>>()
// 新增
const onAdd = () => {
  //ContentAddModalRef.value?.onAdd()
  router.push({ path: '/website/content/add' })
}

// 修改
const onUpdate = (record: ContentResp) => {
  router.push({ path: '/website/content/add', query: { id: record.id, type: 'edit' } })
}

// 详情
const onDetail = (record: ContentResp) => {
  router.push({ path: '/website/content/detail', query: { id: record.id } })
}
</script>

<style lang="scss" scoped></style>
